package com.shujia.core

import org.apache.spark.rdd.RDD
import org.apache.spark.util.LongAccumulator
import org.apache.spark.{SparkConf, SparkContext}

object Code31ComputHelloNum {
  def main(args: Array[String]): Unit = {




    val conf: SparkConf = new SparkConf().setMaster("local").setAppName("Cache")
    val sc = new SparkContext(conf)
    sc.setCheckpointDir("spark_code/output/checkpoint")


    val wordsRDD: RDD[String] = sc.parallelize(
      List("hello spark", "hello scala", "hello world","hello spark", "hello scala", "hello world")
      ,2
    )



    val wordSplitRDD: RDD[String] = wordsRDD.flatMap(
      x => {
        println("flatMap")
        x.split(" ")
      }
    )

    val helloNum: LongAccumulator = sc.longAccumulator("helloNum")



    wordSplitRDD.map(word => {
      if (word == "hello"){
        helloNum.add(1)
        println(s"map => helloNum:${helloNum.value}")
      }
      word
    })
      .foreach(println)

    println(s"helloNum:${helloNum.value}")


  }
}
